package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.Comments;
import com.example.mapper.CommentsMapper;
import com.example.service.CommentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 评论服务实现类
 */
@Service
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments> implements CommentsService {

    @Autowired
    private CommentsMapper commentsMapper;

    /**
     * 获取文章评论列表，包含用户信息（昵称和头像）
     * @param articleId 文章ID
     * @return 包含用户信息的评论列表
     */
    @Override
    public List<Map<String, Object>> getCommentListWithUserInfo(Integer articleId) {
        // 获取文章的所有评论（包括顶级评论和回复）
        List<Map<String, Object>> commentList = commentsMapper.selectCommentListWithUserInfo(articleId);
        
        if (commentList == null || commentList.isEmpty()) {
            return new ArrayList<>();
        }
        
        // 分离顶级评论和回复
        List<Map<String, Object>> topLevelComments = commentList.stream()
                .filter(comment -> comment.get("parent_id") == null || Integer.parseInt(comment.get("parent_id").toString()) == 0)
                .collect(Collectors.toList());
                
        // 将回复添加到对应的顶级评论中
        for (Map<String, Object> topComment : topLevelComments) {
            Integer commentId = Integer.parseInt(topComment.get("id").toString());
            
            // 找到所有回复这个评论的回复
            List<Map<String, Object>> replies = commentList.stream()
                    .filter(reply -> reply.get("parent_id") != null && 
                            Integer.parseInt(reply.get("parent_id").toString()) == commentId)
                    .collect(Collectors.toList());
            
            // 处理回复中的回复对象信息
            for (Map<String, Object> reply : replies) {
                // 如果有回复目标用户ID
                if (reply.get("reply_to_user_id") != null) {
                    Integer replyToUserId = Integer.parseInt(reply.get("reply_to_user_id").toString());
                    
                    // 找到被回复的用户信息
                    commentList.stream()
                            .filter(c -> c.get("user_id") != null && 
                                    Integer.parseInt(c.get("user_id").toString()) == replyToUserId)
                            .findFirst()
                            .ifPresent(replyToUser -> 
                                    reply.put("reply_to_username", replyToUser.get("nick_name") != null ?
                                            replyToUser.get("nick_name") : replyToUser.get("username")));
                }
            }
            
            // 将回复添加到顶级评论中
            topComment.put("replies", replies);
        }
        
        return topLevelComments;
    }
} 